package hu.kisprof.quiz.view.dialogs;

import hu.kisprof.quiz.model.bean.Edu;
import hu.kisprof.quiz.model.bean.Player;
import hu.kisprof.quiz.view.Labels;
import hu.kisprof.quiz.view.QuizGUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class QuizRegistrationDialog extends JDialog implements ActionListener, DocumentListener{


	private static final long serialVersionUID = 356964591409513019L;
	
	private QuizGUI gui;
	
	private JTextField username = new JTextField();
	private JPasswordField password = new JPasswordField();
	private JPasswordField passwordA = new JPasswordField();
	private JTextField realName = new JTextField();
	private JTextField zipcode = new JTextField();
	private JTextField city = new JTextField();
	private JTextField addr = new JTextField();
	private JComboBox edu;
	private JTextField email = new JTextField();
	private JSpinner age = new JSpinner();
	private JComboBox gender = new JComboBox(Labels.genders);
	
	private JButton regButton = new JButton(Labels.registration);
	private JButton cancelButton = new JButton(Labels.cancel);
	
	
	

	public QuizRegistrationDialog(QuizGUI gui){
		super(gui.getWindow(),Labels.registration,true);
		this.gui=gui;
		
		edu = new JComboBox(gui.getControl().getEdus().toArray());
		
		
		JPanel dialogPanel = new JPanel();
		dialogPanel.setLayout(new BorderLayout());
		dialogPanel.add(createSettingsPanel(), BorderLayout.CENTER);
		dialogPanel.add(createButtonsPanel(), BorderLayout.SOUTH);
		
		getContentPane().add(dialogPanel);
        setPreferredSize(new Dimension(300,300));
        pack();
        setLocationRelativeTo(gui.getWindow());
        setVisible(true);
		
		
		
		
		
	}
	
	JPanel createSettingsPanel(){
		JPanel settings = new JPanel(new GridLayout(11,2));
		settings.add(new JLabel(Labels.username+"*"));
		settings.add(username);
		settings.add(new JLabel(Labels.pass+"*"));
		settings.add(password);
		settings.add(new JLabel(Labels.passrepeat+"*"));
		settings.add(passwordA);
		settings.add(new JLabel(Labels.realName));
		settings.add(realName);
		settings.add(new JLabel (Labels.zipcode+"*"));
		zipcode.getDocument().addDocumentListener(this);
		settings.add(zipcode);
		settings.add(new JLabel(Labels.city));
		
		//city.setEditable(false);
		city.setDisabledTextColor(Color.BLACK);
		city.setEnabled(false);
		city.setText(Labels.wrongZipCode);
		city.setBackground(Color.RED);
		
		
		settings.add(city);
		settings.add(new JLabel (Labels.addr));
		settings.add(addr);
		settings.add(new JLabel(Labels.email+"*"));
		settings.add(email);
		settings.add(new JLabel(Labels.edu));
		settings.add(edu);
		settings.add(new JLabel(Labels.age));
		age.setValue(18);
		settings.add(age);
		settings.add(new JLabel(Labels.gender));
		settings.add(gender);		
		return settings;
	}
	
	JPanel createButtonsPanel(){
		JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));
		buttons.add(regButton);
		regButton.addActionListener(this);
		buttons.add(cancelButton);
		cancelButton.addActionListener(this);
		
		
		return buttons;
	}
	
	private boolean checkFields(){
		
		if(username.getText().isEmpty()||
				password.getPassword().length==0||
				passwordA.getPassword().length==0||
				zipcode.getText().isEmpty()||
				email.getText().isEmpty()||
				age.getValue()==null
		){
			JOptionPane.showMessageDialog(this,Labels.mustFillOut,Labels.error, JOptionPane.ERROR_MESSAGE);
			return false;
			
		}		
		if(!Arrays.equals(password.getPassword(),passwordA.getPassword())){
			JOptionPane.showMessageDialog(this,Labels.passAintEqual,Labels.error, JOptionPane.ERROR_MESSAGE);
			return false;
		}
		if(city.getText().equals(Labels.wrongZipCode)){
			JOptionPane.showMessageDialog(this,Labels.zipCodeMustBeGood,Labels.error, JOptionPane.ERROR_MESSAGE);
			return false;		
		}

		return true;
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals(Labels.registration)){
			if (checkFields()){
				Player ply = new Player(username.getText(),
										String.valueOf(password.getPassword()),
										email.getText(),
										realName.getText(),
										Integer.parseInt((zipcode.getText())),
										addr.getText(),
										(Integer)age.getValue(),
										gender.getSelectedItem().toString()==Labels.male?Player.MALE:Player.FEMALE,
										((Edu)edu.getSelectedItem()).getId(),
										0,
										0
										);
				
				
				if(gui.getControl().registerPlayer(ply)){
					JOptionPane.showMessageDialog(this, Labels.succesfulRegistration, Labels.message, JOptionPane.INFORMATION_MESSAGE);
					setVisible(false);
				}else{
					JOptionPane.showMessageDialog(this,Labels.userAlreadyExist,Labels.error, JOptionPane.ERROR_MESSAGE);
				}
			}
		}
		if(e.getActionCommand().equals(Labels.cancel)){
			setVisible(false);
		}
		
		
	}

	@Override
	public void insertUpdate(DocumentEvent e) {
		String tmpCity=gui.getControl().checkZipCode(zipcode.getText());
		if( tmpCity!= null){
			city.setText(tmpCity);
			city.setBackground(Color.GREEN);
			
		}else{
			city.setText(Labels.wrongZipCode);
			city.setBackground(Color.RED);
		}
		
	}

	@Override
	public void removeUpdate(DocumentEvent e) {
		String tmpCity=gui.getControl().checkZipCode(zipcode.getText());
		if( tmpCity!= null){
			city.setText(tmpCity);
			city.setBackground(Color.GREEN);
			
		}else{
			city.setText(Labels.wrongZipCode);
			city.setBackground(Color.RED);
		}
		
	}

	@Override
	public void changedUpdate(DocumentEvent e) {
		String tmpCity=gui.getControl().checkZipCode(zipcode.getText());
		if( tmpCity!= null){
			city.setText(tmpCity);
			city.setBackground(Color.GREEN);
			
		}else{
			city.setText(Labels.wrongZipCode);
			city.setBackground(Color.RED);
		}
		
	}
	
	
	
	
}
